package com.shiqi.service;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.stereotype.Service;

@Service
public class SendSmsImpl  implements SendSms {

    @Override
    public boolean send(String phoneNum, String templateCode, String code) {
        // 连接阿里云
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "这里填自己的accessKeyId", "这里填自己的accessKey密码");
        IAcsClient client = new DefaultAcsClient(profile);

        // 构建请求
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        // 这些内容不要动，是人家阿里爸爸弄出来的，咱不用管
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");

        // 自己的内容,此处 SendSms 为发送验证码
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");

        //自定义的参数(手机号，模板ID，签名 )
        //这是我的内容，请结合你的情况修改为你的东西！！！
        request.putQueryParameter( "PhoneNumbers",""+phoneNum+"");
        request.putQueryParameter( "TemplateCode",""+templateCode+"");
        request.putQueryParameter( "SignName","填自己的签名");
        //构建一个短信的验证码
        request.putQueryParameter("TemplateParam", "{\"code\":\""+code+"\"}");


        try {
            CommonResponse response = client.getCommonResponse(request);
            // 在控制台上打印出返回信息
            System.out.println(response.getData());
            // 返回请求信息是否成功
            return response.getHttpResponse().isSuccess();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }

        return false;
    }
}
